package com.luo.chengrui.module.sa.controller.admin.role;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import com.luo.chengrui.module.sa.controller.admin.role.vo.PermissionCreateReqVO;
import com.luo.chengrui.module.sa.controller.admin.role.vo.PermissionRespVO;
import com.luo.chengrui.module.sa.convert.role.PermissionConvert;
import com.luo.chengrui.module.sa.service.role.PermissionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static java.util.Collections.singleton;

@Tag(name = "管理后台 - 角色菜单权限")
@RestController
@RequestMapping("/sa/permission")
@Validated
public class PermissionController {

    @Resource
    private PermissionService permissionService;

    @PostMapping("/assign-role-menu")
    @Operation(summary = "赋予角色菜单")
    @PreAuthorize("@ss.hasPermission('sa:role:allocation')")
    public CommonResult<Boolean> assignRoleMenu(@Validated @RequestBody PermissionCreateReqVO reqVO) {
        // 执行菜单的分配
        permissionService.assignRoleMenu(reqVO.getRoleId(), reqVO.getMenuIds());
        return success(true);
    }

    @Operation(summary = "获得角色拥有的菜单编号")
    @Parameter(name = "roleId", description = "角色编号", required = true)
    @GetMapping("/list-role-menus")
    @PreAuthorize("@ss.hasPermission('sa:role:allocation')")
    public CommonResult<Set<Long>> getRoleMenuList(String roleId) {
        return success(permissionService.getRoleMenuListByRoleIds(singleton(roleId)));
    }

    @Operation(summary = "获得角色拥有的菜单编号")
    @Parameter(name = "orgFullId", description = "角色编号", required = true)
    @GetMapping("/list-org-menus")
    public CommonResult<List<PermissionRespVO>> getPermissionList(String orgFullId) {
        return success(PermissionConvert.INSTANCE.convertViewList(permissionService.getMenuListByOrgFullId(orgFullId)));
    }

}
